package com.wlyy.bcwlw.sys;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wlyy.bcwlw.common.cache.Cacheable;
import com.wlyy.bcwlw.sys.application.dao.ApplicationDAO;
import com.wlyy.bcwlw.sys.application.entity.ApplicationDTO;
import com.wlyy.bcwlw.sys.menu.dao.MenuDAO;
import com.wlyy.bcwlw.sys.menu.dao.ModuleDAO;
import com.wlyy.bcwlw.sys.menu.entity.MenuDTO;
import com.wlyy.bcwlw.sys.menu.entity.ModuleDTO;
import com.wlyy.bcwlw.sys.permission.dao.PermissionDAO;
import com.wlyy.bcwlw.sys.permission.entity.PermissionDTO;
import com.wlyy.bcwlw.sys.role.dao.RoleDAO;
import com.wlyy.bcwlw.sys.role.entity.RoleDTO;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;
import com.wlyy.bcwlw.sys.user.dao.UserRoleDAO;

/**
 * 
 * @ClassName. CacheHelper
 * @Description. 缓存工具类
 * @author zhangyang
 * @date 2015年8月31日 下午2:33:05
 * @version V1.0
 */
@Service
@Transactional
public class CacheHelper {
	
	private static final Logger logger = LogManager.getLogger(CacheHelper.class);
	
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private MenuDAO menuDAO;
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private ModuleDAO moduleDAO;
	@Autowired
	private PermissionDAO permissionDAO;
	@Autowired
	private ApplicationDAO applicationDAO;
	@Autowired
	private UserRoleDAO userRoleDAO;
	
	/**
	 * 
	* @Title. getUserRoles
	* @Description. 缓存用户对应的角色
	* @param userId
	* @return List<RoleDTO>
	* @exception.
	 */
	@Cacheable(key="userRoleCache",fieldKey="#userId", classType = RoleDTO.class)
	public List<RoleDTO> getUserRoles(String userId) {
		if(logger.isDebugEnabled()) {
			logger.debug("从数据库中查找用户【"+ userId +"】对应的角色");
		}
		return roleDAO.findRoleByUserId(userId);
	}
	
	/**
	 * 
	* @Title. getUserMenuUrls
	* @Description. 缓存用户的对应的菜单
	* @param userId
	* @return List<String>
	* @exception.
	 */
	@Cacheable(key="userMenuUrlsCache",fieldKey="#userId", classType = String.class)
	public List<String> getUserMenuUrls(String userId) {
		if(logger.isDebugEnabled()) {
			logger.debug("从数据库中查找用户【"+ userId +"】对应的菜单");
		}
		List<String> roleIds = userRoleDAO.findUserRoleIds(userId);
		String[] roleArray = roleIds.toArray(new String[0]);
		if(roleArray.length > 0) {
			return menuDAO.getRoleMenuUrls(roleArray);
		}else{
			return new ArrayList<String>(0);
		}
	}
	
	/**
	 * 
	* @Title. getUserMenusByRoleId
	* @Description. 缓存角色对应的菜单
	* @param roleIds
	* @return List<MenuDTO>
	* @exception.
	 */
	@Cacheable(key="userMenusCache",fieldKey="T(org.apache.commons.lang.StringUtils).join(#roleIds, ',')", classType = MenuDTO.class)
	public List<MenuDTO> getUserMenusByRoleId(String[] roleIds) {
		if(logger.isDebugEnabled()) {
			logger.debug("从数据库中查找角色【"+ StringUtils.join(roleIds,",") +"】对应的菜单");
		}
		if(roleIds == null || roleIds.length == 0) {
			return new ArrayList<MenuDTO>(0);
		}
		return menuDAO.getRoleMenus(roleIds);
	}
	
	/**
	 * 
	* @Title. getUserAppsByMenuIds
	* @Description. 从缓存中读取菜单对应的应用
	* @param menuIds
	* @return List<ApplicationDTO>
	* @exception.
	 */
	@Cacheable(key="userAppCache",fieldKey="T(org.apache.commons.lang.StringUtils).join(#menuIds, ',')", classType = ApplicationDTO.class)
	public List<ApplicationDTO> getUserAppsByMenuIds(String[] menuIds){
		if(logger.isDebugEnabled()) {
			logger.debug("从数据库中查找菜单【"+ StringUtils.join(menuIds,",") +"】对应的应用");
		}
		if(menuIds == null || menuIds.length == 0){
			return new ArrayList<ApplicationDTO>();
		}
		return applicationDAO.queryAppByMenuIds(menuIds);
	}
	
	/**
	 * 
	* @Title. getUserModuleByMenuIds
	* @Description. 从缓存中读取菜单对应的模块
	* @param menuIds
	* @return List<ModuleDTO>
	* @exception.
	 */
	@Cacheable(key="userModuleCache",fieldKey="T(org.apache.commons.lang.StringUtils).join(#menuIds, ',')", classType = ModuleDTO.class)
	public List<ModuleDTO> getUserModuleByMenuIds(String[] menuIds){
		if(logger.isDebugEnabled()) {
			logger.debug("从数据库中查找菜单【"+ StringUtils.join(menuIds,",") +"】对应的模块 ");
		}
		if(menuIds == null || menuIds.length == 0){
			return new ArrayList<ModuleDTO>();
		}
		return moduleDAO.queryModuleByMenuIds(menuIds);
	}
	/**
	 * 
	* @Title. getRolePermissions
	* @Description. 读取角色对应的权限
	* @param roleIds
	* @return List<PermissionDTO>
	* @exception.
	 */
	@Cacheable(key="userPermissionCache",fieldKey="T(org.apache.commons.lang.StringUtils).join(#roleIds, ',')", classType = PermissionDTO.class)
	public List<PermissionDTO> getRolePermissions(String[] roleIds) {
		if(logger.isDebugEnabled()) {
			logger.debug("从数据库中查找角色【"+ StringUtils.join(roleIds,",") +"】对应的操作");
		}
		if(roleIds == null || roleIds.length == 0) {
			return new ArrayList<PermissionDTO>(0);
		}
		return permissionDAO.getRolePermissions(roleIds);
	}
	
	
	
}
